class Solution {
public:
    int deleteAndEarn(vector<int>& nums)
    {
        unordered_map<int, int> mp;
        for (auto e : nums) mp[e]++;
        ranges::sort(nums);
        nums.resize(distance(nums.begin(), unique(nums.begin(), nums.end())));
        int n = nums.size();
        vector<int> dp(nums[n - 1] + 1, 0);
        dp[nums[0]] = nums[0] * mp[nums[0]];
        for (int i = nums[0] + 1; i <= nums[n - 1]; i++)
        {
            dp[i] = max(dp[i - 2] + i * mp[i], dp[i - 1]);
        }
        return dp[nums[n - 1]];
    }
};